package com.shrbank.akka

import akka.actor.Actor
import akka.cluster.Cluster
import akka.cluster.ClusterEvent.{MemberUp, UnreachableMember}

/**
  * Created by user on 2016/10/13.
  */
class AkkaClusterMember extends Actor {
  val cluster = Cluster(context.system)
  // 当该节点加入到Cluster中，会获取到其他所有节点发过来的MemberUp消息
  override def preStart(): Unit = cluster.subscribe(self, classOf[MemberUp],classOf[UnreachableMember])
  override def postStop(): Unit = cluster.unsubscribe(self)
  override def receive: Receive = {
    case MemberUp(m) =>
      println(s"member = $m,role=${m.roles.mkString(",")}")
    case UnreachableMember(member) =>
      println("Member detected as unreachable: {},{}", member,member.uniqueAddress)
    case _=>
  }
}
